<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="formData" :inline="true">
        <el-form-item label="设备信息编号" prop="equipCode">
          <el-input
            v-model="formData.equipCode"
            placeholder="请输入设备编信息号"
            clearable
            :disabled="true"
          />
        </el-form-item>
        <el-form-item label="设备信息名称" prop="equipName">
          <el-input
            v-model="formData.equipName"
            placeholder="请输入设备信息名称"
            clearable
            :disabled="true"
          />
        </el-form-item>
        <el-form-item label="设备信息类型" prop="equipLargeCategory">
          <el-input
            v-model="formData.equipTypeName"
            placeholder="请输入设备信息名称"
            clearable
            :disabled="true"
          />
        </el-form-item>
      </el-form>
    </div>

    <el-tabs
      v-model="activeTab"
      class="demo-tabs"
      @tab-change="tabChangeHandle"
    >
      <el-tab-pane
        v-for="item in scaParamTypeList"
        :key="item.value"
        :name="item.value"
      >
        <template #label>
          <span class="custom-tabs-label">
            <span>{{ item.label }}参数</span>
          </span>
        </template>
      </el-tab-pane>
    </el-tabs>

    <el-card shadow="never" class="table-container">
      <template #header>
        <el-button type="success" plain @click="openDialog()">
          <i-ep-plus />
          新增
        </el-button>
        <el-button type="primary" @click="synChronous()">
          同步采集参数
        </el-button>
      </template>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          label="参数名"
          prop="parameterName"
          min-width="120"
          show-overflow-tooltip
          sortable
        />

        <el-table-column
          label="参数描述"
          prop="parameterDesc"
          min-width="120"
          show-overflow-tooltip
        />
        <el-table-column
          label="数据类型"
          prop="parameterType"
          min-width="120"
          align="center"
          show-overflow-tooltip
          sortable
        >
          <template #default="scope">
            {{ useDictionary("ScaDataType", scope.row.parameterType) }}
          </template>
        </el-table-column>
        <el-table-column
          label="采集方式"
          prop="collectionFrequency"
          min-width="120"
          align="center"
          show-overflow-tooltip
          sortable
        >
          <template #default="scope">
            {{
              useDictionary(
                "ScaCollectFrequency",
                scope.row.collectionFrequency
              )
            }}
          </template>
        </el-table-column>
        <el-table-column
          label="采集频率"
          prop="collectionFrequencyTime"
          min-width="120"
          align="center"
          show-overflow-tooltip
          sortable
        >
          <template #default="scope">
            <span v-if="scope.row.collectionFrequencyTime">
              {{ scope.row.collectionFrequencyTime
              }}{{
                scope.row.collectionFrequencyUnit === "s"
                  ? "秒"
                  : scope.row.collectionFrequencyUnit === "m"
                  ? "分钟"
                  : ""
              }}
            </span>
            <span v-else></span>
          </template>
        </el-table-column>
        <el-table-column
          label="参数类别"
          prop="paramType"
          min-width="120"
          align="center"
          show-overflow-tooltip
          sortable
        >
          <template #default="scope">
            {{ useDictionary("ScaParamType", scope.row.paramType) }}
          </template>
        </el-table-column>

        <el-table-column
          label="计量单位"
          prop="measurementUnit"
          min-width="120"
          align="center"
          show-overflow-tooltip
          sortable
        />
        <el-table-column
          label="显示方式"
          prop="showType"
          align="center"
          min-width="120"
          show-overflow-tooltip
          sortable
        >
          <template #default="scope">
            {{ useDictionary("ScaShowType", scope.row.showType) }}
          </template>
        </el-table-column>
        <el-table-column
          label="序号"
          align="center"
          prop="sort"
          min-width="120"
          show-overflow-tooltip
          sortable
        />
        <el-table-column
          label="地址"
          prop="url"
          min-width="120"
          show-overflow-tooltip
          sortable
        />
        <el-table-column
          label="备注"
          prop="remarks"
          min-width="100"
          show-overflow-tooltip
        />

        <el-table-column fixed="right" label="操作" width="200" align="center">
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialog(scope.row)"
            >
              <i-ep-edit />
              编辑
            </el-button>
            <el-button
              type="primary"
              size="small"
              link
              @click="deleteItem(scope.row)"
            >
              <i-ep-delete />
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <scaequipinfoCollect-edit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      @reset-table="(id:string) => getDatas()"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />
  </div>
</template>

<!-- 手动挂载左侧菜单高亮 -->
<script lang="ts">
import { defineComponent } from "vue";
export default defineComponent({
  beforeRouteEnter(to, from, next) {
    to.meta.defaultActive = "/iot/scaequipinfo/content";
    if (!to.query.id) {
      to.meta.title = "采集参数";
    } else {
      to.meta.title = "采集参数";
    }
    next();
  },
});
</script>
<script setup lang="ts">
import { useTable, useDownload, useDictionary } from "@/utils/useTable";
import request from "@/utils/request";
import { useRouter, useRoute } from "vue-router";
import { useDictStoreHook } from "@/store/modules/dictionary"; // 引入
import { Calendar, Document } from "@element-plus/icons-vue";
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "SCAEQUIPINFPCOLLECTPARAMETERS",
  inheritAttrs: false,
});
const route = useRoute();
const router = useRouter();

// 请求参数
const requestApi = "/iotadmin/scaequipcollectionparam/queryByPage";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  paramType: "1",
  relationId: route.query.id,
};

// 初始化搜索参数数据
let formData: any = ref([]);

const deleteParam = {
  api: "/iotadmin/scaequiptype/deleteCollectionParam",
  data: {},
};

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行

// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value
);

const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});

/** 打开设备信息表单弹窗 */
function openDialog(roleObj?: any) {
  if (roleObj) {
    addDialog.data = roleObj;
    addDialog.title = "修改采集参数";
    addDialog.visible = true;
  } else {
    addDialog.data.relationId = route.query.id;
    console.log(addDialog.data);

    addDialog.title = "新增采集参数";
    addDialog.visible = true;
  }
  addDialog.readvisible = false;
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  // getEquipTypeList(route.query.id);
  if (!value) {
    addDialog.data = {};
  }
}

async function deleteItem(row: any) {
  ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    await request({
      url: `/iotadmin/scaequipinfo/deleteCollectionParam`,
      method: "post",
      data: { id: row.id },
    }).then((res) => {
      ElMessage.success("删除成功");
      getEquipTypeList(route.query.id);
      getTableList();
    });
  });
}

// 采集数据列表
// const tableData = ref([]);
const getEquipTypeList = async (id) => {
  const res = await request({
    url: `/iotadmin/scaequipinfo/get/${id}`,
    method: "get",
  });
  if (res.code === 200) {
    formData.value = res.data;
    formData.value.equipTypeName = `${res.data.equipTypeName}-${res.data.equipTypeCode}`;

    console.log(formData.value);

    // tableData.value = res.data.scaEquipCollectionParams;
  }
};

// 同步按钮
const synChronous = () => {
  ElMessageBox.confirm("确认要进行同步码?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(async () => {
    const { data } = await request({
      url: `/iotadmin/scaequipinfo/synchronous/collect/${route.query.id}`,
      method: "get",
    });
    ElMessage.success("同步成功");
    getEquipTypeList(route.query.id);
  });
};

// 更新页面数据
function getDatas() {
  getEquipTypeList(route.query.id);
  getTableList();
}

onMounted(() => {
  if (route.query.id) {
    getDatas();
  }
});

// tab 处理事件
const activeTab = ref("1");
function tabChangeHandle(e: any) {
  pageable.value.pageNum = 1;
  pageable.value.pageSize = 10;
  searchParam.value.paramType = e;
  search();
}

// tab 列表
const scaParamTypeList = ref(dictionaryStore.filterDictList("ScaParamType"));
</script>

<style scoped lang="scss"></style>
